/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bo;

import org.ipti.br.tag.dao.HumanFacade;
import org.ipti.br.tag.entity.Human;
import org.ipti.br.tag.entity.Personage;
import org.ipti.br.tag.entity.Unity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;

/**
 *
 * @author root
 */
@Stateless
public class HumanBO implements Serializable {

    @Inject private HumanFacade humanDAO;
    @Inject private UnityBO unityBO;

    public HumanFacade getHumanDAO() {
        return humanDAO;
    }

    public void create(Human human) {
        getHumanDAO().create(human);
    }

    public void edit(Human human) {
        getHumanDAO().edit(human);
    }

    public void remove(Human human) {
        getHumanDAO().remove2(human);
    }

    public Human find(Object id) {
        return getHumanDAO().find(id);
    }

    public List<Human> findAll() {
        return getHumanDAO().findAll();
    }

    public List<Human> searchHuman(Long unityID){
        return humanDAO.searchHuman(unityID);
    }
    
    public List<Human> searchHumanPerName(String humanName) {
        return getHumanDAO().searchHumanPerName(humanName);
    }

    public List<Human> searchHumanPerName(String humanName, Integer limit) {
        return getHumanDAO().searchLimitedHumanPerName(humanName, limit, null);
    }

    public List<Human> searchAllocatedHuman(String humanName, Personage personage, Unity unity, Integer limit) {
        return getHumanDAO().searchAllocatedHuman(humanName,personage, unity, limit);
    }

    public List<Human> searchHumanMalePerName(String humanName) {
        return getHumanDAO().searchHumanMalePerName(humanName);
    }

    public List<Human> searchHumanFemalePerName(String humanName) {
        return getHumanDAO().searchHumanFemalePerName(humanName);
    }

    public List<Human> modifiedHumanBetweenDate(Calendar initialDate, Calendar finalDate, Long serialNumber) {
        return getHumanDAO().modifiedHumanBetweenDate(initialDate, finalDate, serialNumber);
    }
    
    public List<Human> searchHumanPerPersonageUnityName(String humanName, Long unityID, Long personageID) {
        return getHumanDAO().searchHumanPerPersonageUnityName(humanName, unityID, personageID);
    }
    
    public List<Human> searchHumanPerPersonageUnity(Long unityID, Long personageID) {
        return getHumanDAO().searchHumanPerPersonageUnity(unityID, personageID);
    }

    public List<Human> findAllOrder() {
        return getHumanDAO().findAllOrder();
    }
    
    public List<Human> getActivatedHumans(Unity unity) {
        return getHumanDAO().getActivatedHumans(unity);
    }
    
    public List<Human> getHumans(Unity unity){
        List<Long> unities = unityBO.unityChildrenPerOrganization2(unity.getId(), 2040438459L);
        List<Human> humanList = new ArrayList<>();
        if (!unities.isEmpty()) {
            for (Long id : unities) {
                humanList.addAll(this.searchHuman(id));
            }
        } else {
            humanList.addAll(this.searchHuman(unity.getId()));
        }

        Collections.sort(humanList, new Comparator<Human>() {
                @Override
                public int compare(Human o1, Human o2) {
                    return o1.getName().toLowerCase().compareTo(o2.getName().toLowerCase());
                }
            } );

        
        if (humanList.isEmpty()) {
            return null;
        } else {
            return humanList;
        }
    }
    
}
